﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

using HK2009.Common;
using HK2009.BusinessFacade;
using HK2009.BusinessFacadeHandler;
using HK2009.EntityQuery;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Text;

public partial class SystemManage_MarketEdit : BasePage
{
    ISyMarketHandler marketManager = new SyMarketHandler();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (Session["UserID"] != null)
            {
                ISystemHandler sysManage = new SystemHandler();
                string strPermission = sysManage.GetLoginUserPermissions();
                if (!(strPermission.Contains("1020") || strPermission.Contains("1021")))
                {
                    Response.Redirect("~/NotPurview.aspx");
                    return;
                }
            }           

            dataBind();
            setView();
        }
    }
    private void dataBind()
    {

        if (Request["PKID"] != null)
        {
            Sy_MarketQuery query = marketManager.GetMarketByID(int.Parse(Request["PKID"]));
            if (query != null)
            {
                //txtCode.Text = query.Code;
                txtCNName.Text = query.CNName;
                if (query.Status ==0)
                {
                    radNo.Checked = true;
                    radYes.Checked = false;
                }
                else
                {
                    radNo.Checked = false;
                    radYes.Checked = true;
                }

            }
        }
    }

    private void setView()
    {
        //if (Request["PKID"] != null)
        //{
        //    txtCode.Enabled = false;
        //}
        //else
        //{
        //    txtCode.Enabled = true;
        //}
    }

    protected void btnSave_Click(object sender, EventArgs e)
    {
        Sy_MarketQuery marketQuery = new Sy_MarketQuery();

        marketQuery.CNName = txtCNName.Text.Trim();
        if (radYes.Checked)
        {
            marketQuery.Status = 1;
        }
        else
        {
            marketQuery.Status = 0;
        }
        marketQuery.LastModUser = int.Parse(Session["UserID"].ToString());
        marketQuery.LastModDate = DateTime.Now;

        try
        {
            if (Request["PKID"] != null)
            {
                Sy_MarketQuery tmp = marketManager.GetMarketByID(int.Parse(Request["PKID"]));

                if (tmp.CNName != marketQuery.CNName)
                {
                    SqlParameter[] parms = new SqlParameter[1];
                    parms[0] = new SqlParameter("@Name", marketQuery.CNName);
                    DataTable dt = marketManager.GetList(" select * from Sy_Market where CNName=@Name", parms);
                    if (dt != null && dt.Rows.Count > 0)
                    {
                        Jscript.Alert(Resources.Messages.NameExists, this.Page);
                        txtCNName.Focus();
                        return;
                    }
                }
                if (tmp == null)
                {
                    Jscript.Alert("找不到该记录，更新失败!", this.Page);
                }
                else
                {
                    marketQuery.PKID = tmp.PKID;
                    marketQuery.Code = tmp.Code;
                    if(marketManager.UpdateMarket(marketQuery))
                    {
                        Jscript.AlertAndRedirect(Resources.Messages.UpdateSuccess, "MarketList.aspx", this.Page);
                    }
                    else
                    {
                        Jscript.Alert(Resources.Messages.UpdateFailed, this.Page);
                    }
                }
            }
            else
            {
                //marketQuery.Code = txtCode.Text.Trim();

                SqlParameter[] parms = new SqlParameter[1];
                parms[0] = new SqlParameter("@Name", marketQuery.CNName);
                DataTable dt = marketManager.GetList(" select * from Sy_Market where CNName=@Name", parms);
                if (dt != null && dt.Rows.Count > 0)
                {
                    Jscript.Alert(Resources.Messages.NameExists, this.Page);
                    txtCNName.Focus();
                    return;
                }

                if (marketManager.AddMarket(marketQuery))
                {
                    Jscript.AlertAndRedirect(Resources.Messages.AddSuccess, "MarketList.aspx", this.Page);
                }
                else
                {
                    Jscript.Alert(Resources.Messages.AddFailed, this.Page);
                }
            }
        }
        catch
        {
            Jscript.Alert(Resources.Messages.SaveFailed, this.Page);
        }
        
    }
    protected void btnReset_Click(object sender, EventArgs e)
    {
        Response.Redirect("MarketList.aspx");
    }
}
